package cn.tedu.tedu;
//TODO 迭代 map集合的方式:
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test3 {
    public static void main(String[] args) {
        method();
        method1();
        method2();
    }

    private static void method(){
        //TODO 方式1: keySet() 键集  Set<K> keySet() -- 把map里的key存入set
        Map<Integer,String> m=new HashMap();
        m.put(9527,"唐伯虎");
        m.put(9528,"秋香");
        m.put(9520,"石榴姐");
        m.put(9521,"石榴姐");
        //m.keySet();
        //System.out.println(m.keySet());
        Set<Integer> keys=m.keySet();//把map里的key存入set集合
        // 迭代set,获取每个key
        for(Integer key:keys){
            String value=m.get(key);//拿着key回map里找value
            if( value.equals("秋香姐") ){
                System.out.println(value+"加个微信!");
            }
            System.out.println(key+"~~~"+value);
        }

    }
    private static void method1(){
        //TODO 方式2: entrySet()键值集   Set<Map.Entry<K,V>> entrySet()--
        //把map里的key和value封装成一个Entry对象存入set
        Map<Integer,String> m=new HashMap();
        m.put(9527,"唐伯虎");
        m.put(9528,"秋香");
        m.put(9520,"石榴姐");
        m.put(9521,"石榴姐");
        Set <Map.Entry<Integer,String>>  entrys=m.entrySet();
        //迭代set,获取每个Entry
        for(Map.Entry<Integer,String> entry:entrys){
            Integer key=entry.getKey();//获取Entry封装的key
            String value=entry.getValue();//获取Entry封装的value
            System.out.println(key+"~~~"+value);
        }
        // Map.Entry[] e=new Map.Entry[m.size()];

    }
    private static void method2(){
        //TODO values() 值集
        Map<Integer,String> m=new HashMap();
        m.put(9527,"唐伯虎");
        m.put(9528,"秋香");
        m.put(9520,"石榴姐");
        m.put(9521,"石榴姐");
        Collection<String> values=m.values();
        System.out.println(values);

    }

}
